<?php
namespace app\index\controller;

use app\common\api\BayouSmsApi;
use app\common\api\SmsSingleSender;
use app\common\api\SmsSenderUtil;

class Chat
{
    public function index()
    {
		echo "chat index";
    }
	
	public function add(){
		echo "chat add ";
	}
	
	
	public function push(){
		
		$data = [
            'user' => "用户".rand(0, 2000),
			'title'=>$_POST['content'],
            'content' => $_POST['content'],
        ];
		
		foreach($_POST['http_server']->ports[1]->connections as $fd) {
			$_POST['http_server']->push($fd,$data); 
		}
		
		return Util::show(1, 'ok');
	}
	
	
	/**
	*  腾讯短信服务接口
	*  $mobile   发送信息的电话号码
	*  $templateId  短信模版的 id号
	*  $smsSign  签名
	*  $option   模版参数匹配
	*/
	private function sendByMobile($mobile,$templateId=208087,$smsSign='陕西今朝',$option=array()){
		//1400开头
		$appid = 1400150463; 
		// 短信应用SDK AppKey
		$appkey = "b4e60ac3d2d3b0c4954c74364c481b4b";
		
		$ssender = new SmsSingleSender($appid, $appkey);
		try {
			
			$result = $ssender->sendWithParam("86",$mobile , $templateId, $option, $smsSign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
			return $result;
			
		} catch(\Exception $e) {
			return false;
		}
	}
	
	public function  sendtest(){
		
		//$res = $this->sendByMobile(15349295461,208087,'陕西今朝');  //无内容匹配的内容
		
		$res = $this->sendByMobile(15349295461,361678,'陕西今朝',array('589998',5));   // 匹配验证码的
		// {"result":0,"errmsg":"OK","ext":"","sid":"8:BR6Xu43S8O21FyAXeRj20190627","fee":1}  返回数据 
		print_r($res);
		
	}
}
